<?php
App::uses('AppController', 'Controller');
/**
 * Deliveries Controller
 *
 * @property Delivery $Delivery
 */
class DeliveriesController extends AppController {

/**
 * index method
 *
 * @return void
 */
 
	public function index() {
		$this->Delivery->recursive = 0;
		$this->set('deliveries', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Delivery->exists($id)) {
			throw new NotFoundException(__('Invalid delivery'));
		}
		$options = array('conditions' => array('Delivery.' . $this->Delivery->primaryKey => $id));
		$this->set('delivery', $this->Delivery->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Delivery->create();
					
			
			if ($this->Delivery->save($this->request->data)) {
				$this->Session->setFlash(__('The delivery has been saved'));
				
				$customers = $this->Delivery->getCustomer($this->request->data['Delivery']['order_id']);
				App::uses('CakeEmail', 'Network/Email');
				$email = new CakeEmail();
				$email->from(array('noreply@'.env('SERVER_NAME').'.com' =>  'My Site'));
				$email->to($customers['email']);
				$email->subject('Delivery Update');
				$email->template('delivery');
				$email->emailFormat('html');
		
				$email->viewVars(array(
				'customername' => $customers['first_name']." ".$customers['last_name'],
				'trackingnum' => $this->request->data['Delivery']['tracking'],
				'customerlink' => $this->request->data['Delivery']['customerlink'].$customers['id'])
				);
						
				$email->send();		
				
				
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The delivery could not be saved. Please, try again.'));
			}
		}
		$orders = $this->Delivery->Order->find('list');
		$items = $this->Delivery->Item->find('list');
		$deliveryStatuses = $this->Delivery->DeliveryStatus->find('list');
		$this->set(compact('orders', 'items', 'deliveryStatuses'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Delivery->exists($id)) {
			throw new NotFoundException(__('Invalid delivery'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Delivery->save($this->request->data)) {
				$this->Session->setFlash(__('The delivery has been saved'));
				
				App::uses('CakeEmail', 'Network/Email');
				$email = new CakeEmail();
				$email->from(array('noreply@'.env('SERVER_NAME').'.com' =>  'My Site'));
				$email->to($this->request->data['Delivery']['toemail']);
				$email->subject('Delivery Update');
				$email->template('delivery');
				$email->emailFormat('html');
		
				$email->viewVars(array(
				'customername' => $this->request->data['Delivery']['customername'],
				'trackingnum' => $this->request->data['Delivery']['tracking'],
				'customerlink' => $this->request->data['Delivery']['customerlink'])
				);
						
				$email->send();				
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The delivery could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Delivery.' . $this->Delivery->primaryKey => $id));

			$this->request->data = $this->Delivery->find('first', $options);
			$this->set('customername',$this->request->data['Customer']['first_name']." ".$this->request->data['Customer']['last_name']);
			$this->set('customerid',$this->request->data['Customer']['id']);
			$this->set('toemail',$this->request->data['Customer']['email']);
		}
		$orders = $this->Delivery->Order->find('list');
		$items = $this->Delivery->Item->find('list');
		$deliveryStatuses = $this->Delivery->DeliveryStatus->find('list');

		$this->set(compact('orders', 'items', 'deliveryStatuses'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Delivery->id = $id;
		if (!$this->Delivery->exists()) {
			throw new NotFoundException(__('Invalid delivery'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Delivery->delete()) {
			$this->Session->setFlash(__('Delivery deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Delivery was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
